package com.atguigu.serviceorder.service.impl;

import com.atguigu.common.vo.CourseWebOrderVo;
import com.atguigu.common.vo.UcenterMemberOrderVo;
import com.atguigu.serviceorder.entity.Order;
import com.atguigu.serviceorder.feign.ServiceEduFeign;
import com.atguigu.serviceorder.feign.ServiceUcenterFeign;
import com.atguigu.serviceorder.mapper.OrderMapper;
import com.atguigu.serviceorder.service.OrderService;
import com.atguigu.serviceorder.util.OrderNoUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 订单 服务实现类
 * @author yuzhen
 */
@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements OrderService {

    @Autowired
    private ServiceEduFeign serviceEduFeign;
    @Autowired
    private ServiceUcenterFeign serviceUcenterFeign;

    /**
     * 生成订单的方法
     */
    @Override
    public String createOrders(String courseId, String memberId) {
        // 通过远程调用根据用户id获取用户信息
        UcenterMemberOrderVo userInfoOrder = serviceUcenterFeign.getUserInfoOrder(memberId);

        // 通过远程调用根据课程id获取课信息
        CourseWebOrderVo courseInfoOrder = serviceEduFeign.getCourseInfoOrder(courseId);

        // 创建Order对象，向order对象里面设置需要数据
        Order order = new Order();
        order.setOrderNo(OrderNoUtil.getOrderNo());// 订单号
        order.setCourseId(courseId);// 课程id
        order.setCourseTitle(courseInfoOrder.getTitle());
        order.setCourseCover(courseInfoOrder.getCover());
        order.setTeacherName(courseInfoOrder.getTeacherName());
        order.setTotalFee(courseInfoOrder.getPrice());
        order.setMemberId(memberId);
        order.setMobile(userInfoOrder.getMobile());
        order.setNickname(userInfoOrder.getNickname());
        order.setStatus(0);// 订单状态（0：未支付 1：已支付）
        order.setPayType(1);// 支付类型 ，微信1
        baseMapper.insert(order);
        // 返回订单号
        return order.getOrderNo();
    }

}
